//创建专业相关小仓库
import { defineStore } from "pinia";
import { reqMajorAll, reqMajorAdd, reqMajorList, reqMajorById, reqMajorDelete, reqMajorUpdate } from "@/api/major";
import type {
    majorInfoData, majorListResponseData
    , majorResponseData, majorAllResponseData, majorListFormData
} from "@/api/major/type";
import type{ MajorState } from "./types/types";
let useMajorStore = defineStore('Major', {
    state: (): MajorState => {
        return {
            majors: []
        }
    },
    actions: {
        //获取所有专业
        async getMajorAll() {
            let result:majorAllResponseData=await reqMajorAll();
            if(result.status == 0){
                this.majors=(result.data as majorInfoData[])
                return  'ok'
            }else{
                return Promise.reject(new Error (result.msg))
            }
        },
        //分页获取专业
        async getMajorList(data:majorListFormData){
            let result :majorListResponseData= await reqMajorList(data);
            if(result.status == 0){
                this.majors=(result.data?.data as majorInfoData[])
                return result.data
            }else{
                return Promise.reject(new Error (result.msg))
            }
        },
        //添加专业
        async addMajor(data:majorInfoData){
            let result :majorResponseData= await reqMajorAdd(data);
            if(result.status == 0){
                return "ok"
            }else{
                return Promise.reject(new Error (result.msg))
            }
        },
        //根据id查询专业
        async getMajorById(_id:string){
            let result :majorResponseData= await reqMajorById(_id);
            if(result.status == 0){
                return result.data
            }else{
                return Promise.reject(new Error (result.msg))
            }
        },
        //更新专业信息
        async updateMajor(data:majorInfoData){
            let result :majorResponseData= await reqMajorUpdate(data);
            if(result.status == 0){
                return "ok"
            }else{
                return Promise.reject(new Error (result.msg))
            }
        },
        //删除专业
         async deleteMajor(majorId:string){
            let result :majorResponseData= await reqMajorDelete(majorId);
            if(result.status == 0){
                return "ok"
            }else{
                return Promise.reject(new Error (result.msg))
            }
        },
    },
})
export default useMajorStore
























